package userConsole.servlet;

import org.json.JSONException;
import org.json.JSONObject;
import userConsole.service.InsertUserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author Godv
 * @version 1.0
 */
@WebServlet("/insertUserServlet")
public class InsertUserServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String age = req.getParameter("age");
        InsertUserService insertUserService = new InsertUserService();
        try {
            JSONObject jsonObject = insertUserService.insertUser(username, password, age);
            resp.setContentType("application/json;charset=utf-8");
            PrintWriter writer = resp.getWriter();
            writer.print(jsonObject);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}
